home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GDEVDJET.C < prev    next >
C/C++ Source or Header  |  1992-02-17  |  12KB  |  372 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevdjet.c */
  21. /* HP LaserJet/DeskJet driver for Ghostscript */
  22. #include "gdevprn.h"
  23. #include "gdevpcl.h"
  24.  
  25. /* Thanks to Jim Mayer, Xerox Webster Research Center, */
  26. /* and to Jan-Mark Wams (jms@cs.vu.nl) for improvements. */
  27.  
  28. /*
  29.  * You may select a resolution of 75, 100, 150, or 300 DPI.
  30.  * Normally you would do this in the makefile or on the gs command line,
  31.  * not here.
  32.  */
  33. /*#define X_DPI 300*/
  34. /*#define Y_DPI 300*/
  35.  
  36. /*
  37.  * Standard U.S. page width and height.  A4 paper is 8.4" x 11.7".
  38.  */
  39. #define WIDTH_10THS 85
  40. #define HEIGHT_10THS 110
  41.  
  42. #define X_DPI_MAX 300
  43. #define Y_DPI_MAX 300
  44. /* We round up the LINE_SIZE to a multiple of a ulong for faster scanning. */
  45. #define W sizeof(word)
  46. #define LINE_SIZE ((X_DPI_MAX * 85 / 10 + W * 8 - 1) / (W * 8) * W)
  47. #ifndef X_DPI
  48. #  define X_DPI X_DPI_MAX
  49. #endif
  50. #ifndef Y_DPI
  51. #  define Y_DPI Y_DPI_MAX
  52. #endif
  53.  
  54. /* Printer types */
  55. #define LJ    0
  56. #define LJplus    1
  57. #define LJ2p    2
  58. #define LJ3    3
  59. #define DJ    4
  60. #define DJ500    5
  61.  
  62. /* The printer initialization strings. */
  63. private char *init_strings[] = {
  64.     /* LaserJet PCL 3, no compression */
  65.         "\033*r0A\033&l0E\033*p0x0Y\033*b0M",
  66.     /* LaserJet Plus PCL 3, no compression */
  67.         "\033*r0A\033&l0E\033*p0x0Y\033*b0M",
  68.     /* LaserJet IIP PCL 4, mode 2 compression */
  69.         "\033*r0f0A\033&l0E\033&l-185U\033*p0x0Y\033*b2M",
  70.     /* LaserJet III PCL 5, mode 2&3 compression */
  71.         "\033*r0f0A\033&l0E\033&l-185U\033*p0x0Y",
  72.     /* DeskJet almost PCL 4, mode 2 compression */
  73.         /* Key to codes: set 8.5x11 paper; portrait orientation; */
  74.         /* bidirectional printing; no perf-skip; move to top left; */
  75.         /* start raster graphics; mode 2 compression. */
  76.         "\033&l2A\033&l0O\033&k1W\033&l0L\033*p0x0Y\033*r0A\033*b2M",
  77.     /* DeskJet 500 almost PCL 4, mode 2&3 compression */
  78.         "\033&l2A\033&l0O\033&k1W\033&l0L\033*p0x0Y\033*r0A",
  79. };
  80.  
  81. /* The patterns for skipping blank lines. */
  82. private char *skip_patterns[] = {
  83.     /* LaserJet PCL 3 */
  84.         "\033*p+%dY",
  85.     /* LaserJet Plus PCL 3 */
  86.         "\033*p+%dY",
  87.     /* LaserJet IIP PCL 4 */
  88.         "\033*b%dY",
  89.     /* LaserJet III PCL 5 */
  90.         "\033*b%dY",
  91.     /* DeskJet almost PCL 4 */
  92.         "\033*b%dY",
  93.     /* DeskJet 500 almost PCL 4 */
  94.         "\033*b%dY"
  95. };
  96.  
  97. /* The device descriptors */
  98. private dev_proc_print_page(djet_print_page);
  99. private dev_proc_print_page(djet500_print_page);
  100. private dev_proc_print_page(ljet_print_page);
  101. private dev_proc_print_page(ljetplus_print_page);
  102. private dev_proc_print_page(ljet2p_print_page);
  103. private dev_proc_print_page(ljet3_print_page);
  104.  
  105. /*
  106.  * HP printers need their own get_initial_matrix procedure.  See the
  107.  * definition of hp_get_initial_matrix for the reason why.
  108.  */
  109. private dev_proc_get_initial_matrix(hp_get_initial_matrix);
  110.  
  111. /* See gdevprn.h and gdevprn.c to understand the NULL entries below. */
  112. gx_device_procs prn_hp_procs = {
  113.     gdev_prn_open,
  114.     hp_get_initial_matrix,
  115.     gx_default_sync_output,
  116.     gdev_prn_output_page,
  117.     gdev_prn_close,
  118.     gdev_prn_map_rgb_color,
  119.     gdev_prn_map_color_rgb,
  120.     NULL,    /* fill_rectangle */
  121.     NULL,    /* tile_rectangle */
  122.     NULL,    /* copy_mono */
  123.     NULL,    /* copy_color */
  124.     NULL,    /* draw_line */
  125.     NULL,    /* get_bits */
  126.     gdev_prn_get_props,
  127.     gdev_prn_put_props
  128. };
  129.  
  130. gx_device_printer gs_deskjet_device =
  131.   prn_device(prn_hp_procs, "deskjet",
  132.     WIDTH_10THS, HEIGHT_10THS,
  133.     X_DPI, Y_DPI,
  134.     0.25, 0.50, 0.25, 0.0625,    /* margins */
  135.     1, djet_print_page);
  136.  
  137. gx_device_printer gs_djet500_device =
  138.   prn_device(prn_hp_procs, "djet500",
  139.     WIDTH_10THS, HEIGHT_10THS,
  140.     X_DPI, Y_DPI,
  141.     0.25, 0.50, 0.25, 0.0625,    /* margins */
  142.     1, djet500_print_page);
  143.  
  144. gx_device_printer gs_laserjet_device =
  145.   prn_device(prn_hp_procs, "laserjet",
  146.     WIDTH_10THS, HEIGHT_10THS,
  147.     X_DPI, Y_DPI,
  148.     0.05, 0.25, 0.55, 0.25,        /* margins */
  149.     1, ljet_print_page);
  150.  
  151. gx_device_printer gs_ljetplus_device =
  152.   prn_device(prn_hp_procs, "ljetplus",
  153.     WIDTH_10THS, HEIGHT_10THS,
  154.     X_DPI, Y_DPI,
  155.     0.05, 0.25, 0.55, 0.25,        /* margins */
  156.     1, ljetplus_print_page);
  157.  
  158. gx_device_printer gs_ljet2p_device =
  159.   prn_device(prn_hp_procs, "ljet2p",
  160.     WIDTH_10THS, HEIGHT_10THS,
  161.     X_DPI, Y_DPI,
  162.     0.20, 0.25, 0.25, 0.25,        /* margins */
  163.     1, ljet2p_print_page);
  164.  
  165. gx_device_printer gs_ljet3_device =
  166.   prn_device(prn_hp_procs, "ljet3",
  167.     WIDTH_10THS, HEIGHT_10THS,
  168.     X_DPI, Y_DPI,
  169.     0.20, 0.25, 0.25, 0.25,        /* margins */
  170.     1, ljet3_print_page);
  171.  
  172. /* Forward references */
  173. private int hpjet_print_page(P3(gx_device_printer *, FILE *, int));
  174.  
  175. /* ------ Internal routines ------ */
  176.  
  177. /* The DeskJet can compress (mode 2) */
  178. private int
  179. djet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  180. {    return hpjet_print_page(pdev, prn_stream, DJ);
  181. }
  182. /* The DeskJet500 can compress (mode 3) */
  183. private int
  184. djet500_print_page(gx_device_printer *pdev, FILE *prn_stream)
  185. {    return hpjet_print_page(pdev, prn_stream, DJ500);
  186. }
  187. /* The LaserJet series II can't compress */
  188. private int
  189. ljet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  190. {    return hpjet_print_page(pdev, prn_stream, LJ);
  191. }
  192. /* The LaserJet Plus can't compress */
  193. private int
  194. ljetplus_print_page(gx_device_printer *pdev, FILE *prn_stream)
  195. {    return hpjet_print_page(pdev, prn_stream, LJplus);
  196. }
  197. /* All LaserJet series IIIs (III,IIId,IIIp,IIIsi) compress (mode 3) */
  198. private int
  199. ljet3_print_page(gx_device_printer *pdev, FILE *prn_stream)
  200. {    return hpjet_print_page(pdev, prn_stream, LJ3);
  201. }
  202. /* LaserJet series IIp & IId compress (mode 2) */
  203. private int
  204. ljet2p_print_page(gx_device_printer *pdev, FILE *prn_stream)
  205. {    return hpjet_print_page(pdev, prn_stream, LJ2p);
  206. }
  207.  
  208. /* Send the page to the printer.  For speed, compress each scan line, */
  209. /* since computer-to-printer communication time is often a bottleneck. */
  210. private int
  211. hpjet_print_page(gx_device_printer *pdev, FILE *prn_stream, int compress)
  212. {    int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  213.     int line_size_words = (line_size + W - 1) / W;
  214.     uint storage_size_words = line_size_words * 8; /* data, out_row, out_row_alt, prev_row */
  215.     word *storage = (ulong *)gs_malloc(storage_size_words, W,
  216.                        "hpjet_print_page");
  217.     word
  218.       *data_words,
  219.       *out_row_words,
  220.       *out_row_alt_words,
  221.       *prev_row_words;
  222. #define data ((char *)data_words)
  223. #define out_row ((char *)out_row_words)
  224. #define out_row_alt ((char *)out_row_alt_words)
  225. #define prev_row ((char *)prev_row_words)
  226.     char *out_data;
  227.     int x_dpi = pdev->x_pixels_per_inch;
  228.     int out_count;
  229.     int compression = -1;
  230.  
  231.     if ( storage == 0 ) return -1; /* can't allocate working area */
  232.     data_words = storage;
  233.     out_row_words = data_words + (line_size_words * 2);
  234.     out_row_alt_words = out_row_words + (line_size_words * 2);
  235.     prev_row_words = out_row_alt_words + (line_size_words * 2);
  236.     /* Clear temp storage */
  237.     memset(data, 0, storage_size_words * W);
  238.  
  239.     /* Initialize printer. */
  240.     fputs("\033E", prn_stream);        /* reset printer */
  241.     fputs("\033*rB", prn_stream);        /* end raster graphics */
  242.     fprintf(prn_stream, "\033*t%dR", x_dpi);    /* set resolution */
  243.     fputs(init_strings[compress], prn_stream);
  244.  
  245.     /* Send each scan line in turn */
  246.        {    int lnum;
  247.         int num_blank_lines = 0;
  248.         word rmask = ~(word)0 << (-pdev->width & (W * 8 - 1));
  249.         /* look for top margin white space... You would think that
  250.            the normal (raster) white space mechanism would work... it
  251.            doesn't... Sometimes PCL printers are brain-dead */
  252.         for ( lnum = 0; lnum < pdev->height; lnum++ )
  253.            {    register word *end_data = data_words + line_size_words;
  254.             gdev_prn_copy_scan_lines(pdev, lnum,
  255.                          (byte *)data, line_size);
  256.                /* Mask off 1-bits beyond the line width. */
  257.             end_data[-1] &= rmask;
  258.             /* Remove trailing 0s. */
  259.             while ( end_data > data_words && end_data[-1] == 0 )
  260.               end_data--;
  261.             if ( end_data == data_words )
  262.                /* Blank line */
  263.                num_blank_lines++;
  264.             else
  265.                break;
  266.            }
  267.         /* Skip blank lines if any */
  268.         if ( num_blank_lines > 0 )
  269.           {    /* move down from current position */
  270.                  fprintf(prn_stream,"\033*p+%dY", num_blank_lines);
  271.                  num_blank_lines = 0;
  272.           }
  273.              /* transfer raster graphics */
  274.         for ( ; lnum < pdev->height; lnum++ )
  275.            {    register word *end_data = data_words + line_size_words;
  276.             gdev_prn_copy_scan_lines(pdev, lnum,
  277.                          (byte *)data, line_size);
  278.                /* Mask off 1-bits beyond the line width. */
  279.             end_data[-1] &= rmask;
  280.             /* Remove trailing 0s. */
  281.             while ( end_data > data_words && end_data[-1] == 0 )
  282.               end_data--;
  283.             if ( end_data == data_words )
  284.                {    /* Blank line */
  285.                 num_blank_lines++;
  286.                 continue;
  287.                }
  288.             switch (compress)
  289.               {
  290.               case LJ3:
  291.               case DJ500:
  292.                {    /* Compression modes 2 and 3 are both */
  293.                 /* available.  Try both and see which one */
  294.                 /* produces the least output data. */
  295.                 int count3 = gdev_pcl_mode3compress(line_size, data,
  296.                                prev_row, out_row);
  297.                 int count2 = gdev_pcl_mode2compress(data_words, end_data,
  298.                                out_row_alt);
  299.                 int penalty3 = (compression == 3 ? 0 : 5);
  300.                 int penalty2 = (compression == 2 ? 0 : 5);
  301.                 if ( count3 + penalty3 < count2 + penalty2)
  302.                    {    if ( compression != 3 )
  303.                         fputs("\033*b3M", prn_stream);
  304.                     compression = 3;
  305.                     out_data = out_row;
  306.                     out_count = count3;
  307.                    }
  308.                 else
  309.                    {    if ( compression != 2 )
  310.                         fputs("\033*b2M", prn_stream);
  311.                     compression = 2;
  312.                     out_data = out_row_alt;
  313.                     out_count = count2;
  314.                    }
  315.                 break;
  316.                }
  317.               case DJ:
  318.               case LJ2p:
  319.                 out_data = out_row;
  320.                    out_count = gdev_pcl_mode2compress(data_words, end_data,
  321.                               out_row);
  322.                 break;
  323.               default:
  324.                 out_data = data;
  325.                 out_count = (char *)end_data - data;
  326.               }
  327.             /* Skip blank lines if any */
  328.             if ( num_blank_lines > 0 )
  329.                {    /* move down from current position */
  330.                 fprintf(prn_stream, skip_patterns[compress],
  331.                     num_blank_lines);
  332.                 num_blank_lines = 0;
  333.                }
  334.             /* transfer raster graphics */
  335.             fprintf(prn_stream, "\033*b%dW", out_count);
  336.             /* send the row */
  337.             fwrite(out_data, sizeof(char), out_count,
  338.                    prn_stream);
  339.            }
  340.     }
  341.  
  342.     /* end raster graphics */
  343.     fputs("\033*rB", prn_stream);
  344.  
  345.     /* eject page */
  346.     fputs("\033&l0H", prn_stream);
  347.  
  348.     /* free temporary storage */
  349.     gs_free((char *)storage, storage_size_words, W, "hpjet_print_page");
  350.  
  351.     return 0;
  352. }
  353.  
  354.  
  355. /*
  356.  * PCL has the notion of a LOGICAL and a PHYSICAL page.  The PHYSICAL page is
  357.  * the actual paper; the LOGICAL page is the printer specific imageable area.
  358.  * The strange part is that coordinates are all relative to the
  359.  * LOGICAL page.  This means that all PCL code is inherently device dependent.
  360.  * Luckily, in PostScript land, transformation matrices conquer all.
  361.  */
  362.  
  363. private void
  364. hp_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  365. {       pmat->xx = dev->x_pixels_per_inch / 72.0;
  366.     pmat->xy = 0;
  367.     pmat->yx = 0;
  368.     pmat->yy = dev->y_pixels_per_inch / -72.0;
  369.     pmat->tx = -(dev->l_margin * dev->x_pixels_per_inch);
  370.     pmat->ty = dev->height - (dev->t_margin * dev->y_pixels_per_inch);
  371. }
  372.